pub use shq_common::prelude::{
    init_app_config, init_page_config, init_regex_config, reload_page_config, Error, Result,
};
use shq_common::prelude::{init_cache, Deptment, MenuItem, Permission, Role, TreeNode};
use shq_report::init_rept_config;

pub async fn init() {
    init_app_config().await.unwrap();
    init_page_config().await.unwrap();
    init_rept_config().await.unwrap();
    init_regex_config().unwrap();
    shq_auth::init_menus_conf().unwrap();
    init_cache()
        .await
        .or_else(|e| {
            log::error!("Failed to initialize cache: {}", e);
            Err(e)
        })
        .unwrap();
    shq_workflow::init_workflow_config().await.unwrap();
}
pub fn init_menus_conf() -> anyhow::Result<()> {
    shq_auth::init_menus_conf()
}
pub fn get_menus() -> anyhow::Result<Vec<MenuItem>> {
    shq_auth::get_menus()
}
pub async fn get_menus_user(user_id: u32) -> anyhow::Result<Vec<MenuItem>> {
    shq_auth::get_menus_user_id(user_id).await
}
//获取角色
pub async fn get_roles() -> anyhow::Result<Vec<Role>> {
    shq_auth::get_roles().await
}
// 获取用户
pub async fn get_user() -> anyhow::Result<Vec<TreeNode>> {
    shq_auth::get_user().await
}

// 根据所选角色id来获取权限
pub async fn get_role_perms(role_id: u32) -> anyhow::Result<Vec<(u32, Permission)>> {
    shq_auth::get_perms_by_role_id(role_id).await
}

// 根据所选角色id来获取权限
pub async fn get_user_perms(user_id: u32) -> anyhow::Result<Vec<(u32, Permission)>> {
    shq_auth::get_perms_by_user_id(user_id).await
}

// 根据所选角色id来获取权限
pub async fn get_role_users(role_id: u32) -> anyhow::Result<Vec<u32>> {
    shq_auth::get_users_by_role_id(role_id).await
}
pub async fn get_user_roles(user_id: u32) -> anyhow::Result<Vec<u32>> {
    shq_auth::get_roles_by_user_id(user_id).await
}
// 保存角色菜单权限
pub async fn save_role_perms(role_id: u32, perms: Vec<(u32, Permission)>) -> anyhow::Result<()> {
    shq_auth::perms_to_role(role_id, perms).await
}
// 保存角色用户权限
pub async fn save_role_users(role_id: u32, user_ids: Vec<u32>) -> anyhow::Result<()> {
    shq_auth::users_to_role(role_id, user_ids).await
}
// 保存用户菜单权限
pub async fn save_user_perms(user_id: u32, perms: Vec<(u32, Permission)>) -> anyhow::Result<()> {
    shq_auth::perms_to_user(user_id, perms).await
}
// 保存用户角色权限
pub async fn save_user_roles(user_id: u32, role_ids: Vec<u32>) -> anyhow::Result<()> {
    shq_auth::roles_to_user(user_id, role_ids).await
}
//获取部门
pub async fn get_deptment() -> anyhow::Result<Vec<Deptment>> {
    shq_auth::get_deptment_list().await
}
/// 获取菜单权限
/// menu_id:菜单id
/// 返回值:菜单权限列表
/// 例子: get_menu_perms(100)
/// 返回值:菜单权限列表
pub async fn get_menu_perms(user_id: u32, menu_id: u32) -> anyhow::Result<Option<Permission>> {
    shq_auth::get_user_perm(user_id, menu_id).await
}
// 清除角色用户权限
pub async fn del_role(role_id: u32) -> anyhow::Result<()> {
    shq_auth::del_role(role_id).await
}
pub async fn get_auth_type(user_id: u32, menu_id: u32) -> anyhow::Result<Option<Permission>> {
    shq_auth::get_user_perm(user_id, menu_id).await
}

pub async fn del_user(user_id: u32) -> anyhow::Result<()> {
    shq_auth::del_user(user_id).await
}

pub async fn user_login(
    user_no: &str,
    user_pwd: &str,
) -> anyhow::Result<shq_common::prelude::User> {
    shq_auth::user_login(user_no, user_pwd).await
}

pub async fn change_password(
    user_id: u32,
    old_pwd: &str,
    new_pwd: &str,
) -> anyhow::Result<()> {
    shq_auth::change_password(user_id, old_pwd, new_pwd).await
}

pub async fn init_password(
    user_id: u32,
) -> anyhow::Result<()> {
    shq_auth::init_password(user_id).await
}

// pub async fn get_kpi_import_conf_name(jd: i32, conf_name: &str) -> anyhow::Result<Vec<KpiImportConfName>> {
//     let result = shq_auth::kpi_import_conf_name(jd, conf_name).await?;
//     if result.len() == 0 {
//         return Err(anyhow::anyhow!("没有找到对应配置项"));
//     }
//     Ok(result)
// }
//
// pub async fn get_kpi_import_log(emid: u32, zbid: i32,jd: i32) -> anyhow::Result<()> {
//     let result = shq_auth::kpi_import_log(emid, zbid,jd).await?;
//     Ok(result)
// }
